<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="zf" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 主体部分 -->
        <el-tree :data="list" default-expand-all>
          <template v-slot="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width:100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hPutdePartments(data.id)">编辑</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(data.id)">删除</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        title="增加"
        :visible.sync="showDialog"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="50%"
      >
        <add-or-edit v-if="showDialog" :id="childrenId" :origin-list="originList" :is-edit="isEdit" @hCancels="hCancel" @success="hSuccess" />
      </el-dialog>

      <el-dialog
        title="编辑"
        :visible.sync="showPutDialog"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="50%"
      >
        <add-or-edit v-if="showPutDialog" :id="childrenId" :origin-list="originList" :is-edit="isEdit" @hCancels="hCancel" @success="hSuccess" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartmentsList, delCompanyDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import addOrEdit from './depDialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      list: [],
      showDialog: false,
      showPutDialog: false,
      childrenId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartmentsList()
  },
  methods: {
    handleNodeClick(data) {
      console.log(data)
    },
    // 渲染页面列表
    async loadDepartmentsList() {
      const res = await getDepartmentsList()
      // console.log(res.data.depts)
      res.data.depts.shift(1)
      this.originList = res.data.depts.map(item => {
        return {
          id: item.id, name: item.name, code: item.code, pid: item.pid
        }
      })
      // console.log(this.originList)
      this.list = tranListToTreeData(res.data.depts)
    },
    // 添加子部门
    hAdd(id) {
      // console.log(id)
      this.isEdit = false
      this.showDialog = true
      // 获取 ID 传给 子组件
      this.childrenId = id
    },
    hSuccess() {
      this.showDialog = false
      this.showPutDialog = false
      this.loadDepartmentsList()
    },
    // 编辑部门
    hPutdePartments(id) {
      this.isEdit = true
      this.showPutDialog = true
      this.childrenId = id
    },
    // 删除部门
    hDel(id) {
      this.$confirm('确认删除', '提示', { type: 'warning' })
        .then(async() => {
          try {
            const res = await delCompanyDepartments(id)
            this.$message.success(res.message)
            this.loadDepartmentsList()
          } catch (e) {
            this.$message.error(e.message)
          }
        })
        .catch(() => {})
    },
    hCancel() {
      this.showDialog = false
      this.showPutDialog = false
    }
  }
}
</script>
